package com.start_eternity.oasissafewalk.controller;

import com.start_eternity.oasissafewalk.comment.ErrorCode;
import com.start_eternity.oasissafewalk.comment.ResultUtils;
import com.start_eternity.oasissafewalk.pojo.OrderDetails;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.service.OrderDetailsService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * author:ZhangT
 * Date:2024 / 03 / 30 / 11:27
 * Description:
 */
@Slf4j
@RestController
@RequestMapping("orderDetails")
public class OrderDetailsController {

    @Resource
    private OrderDetailsService orderDetailsService;

    //查询 根据order_details_id
    @GetMapping("/checkId")
    public Result<OrderDetails> getById(@RequestParam Integer id) {
        OrderDetails orderDetails = orderDetailsService.queryById(id);
        if (orderDetails != null) {
            return ResultUtils.success(orderDetails);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }

    //查询 根据order_no_id
    @GetMapping("/checkNo")
    public Result<OrderDetails> getByNo(@RequestParam String no) {
        OrderDetails orderDetails = orderDetailsService.queryByNo(no);
        if (orderDetails != null) {
            return ResultUtils.success(orderDetails);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }
    //添加
    @PostMapping("/save")
    public Result<Integer> saveDetails(@RequestBody OrderDetails orderDetails) {
        int i = orderDetailsService.insertDetails(orderDetails);
        if (i == 0) {
            return ResultUtils.error(ErrorCode.FAIL_INSERT);
        }
        return ResultUtils.success(i);
    }

    //修改 根据 order_details_id
    @PutMapping("/updateId")
    public Result<Integer> updateId(@RequestBody OrderDetails orderDetails){
        log.info("更新single_orders信息:{}",orderDetails);
        int i = orderDetailsService.updateDetailsById(orderDetails);
        if (i == 0) {
            return ResultUtils.error(ErrorCode.FAIL_UPDATE);
        }
        return ResultUtils.success(i);
    }


    //修改 根据 order_no_id
    @PutMapping("/updateNo")
    public Result<Integer> updateNo(@RequestBody OrderDetails orderDetails){
        log.info("更新single_orders信息:{}",orderDetails);
        int i = orderDetailsService.updateDetailsByNo(orderDetails);
        if (i == 0) {
            return ResultUtils.error(ErrorCode.FAIL_UPDATE);
        }
        return ResultUtils.success(i);
    }
}
